package com.iwish.server.model;

import org.apache.log4j.Logger;
import org.apache.mina.core.session.IoSession;

import com.iwish.server.storage.model.Obj;
import com.iwish.server.util.Base64Util;
import com.iwish.server.util.RequestCode;
import com.iwish.server.util.Separator;

public class UpdateUserInfoService extends ServiceTemplate{

	Logger logger = Logger.getLogger(UpdateUserInfoService.class);
	
	private String uid;
	private String nickname;
	private boolean gender ;
	private String img;
	private String date_of_birth;
	
	public UpdateUserInfoService(){}
	
	public UpdateUserInfoService(String uid, String nickname, boolean gender, String date_of_birth, String img)
	{
		this.uid = uid;
		this.nickname = nickname;
		this.gender = gender;
		this.date_of_birth = date_of_birth;
		this.img = img;
	}
	
	public boolean update(IoSession session)
	{
		Object u = get(uid, Obj.User.class, 30);
		if(! (u instanceof Obj.User))
		{
			String result = Base64Util.encode(RequestCode.UPDATE_USER_INFO_RESULT.getBytes())
					+ Separator.F_SEPARATOR
					+ Base64Util.encode(RequestCode.FAIL.getBytes())
					+ Separator.F_SEPARATOR
					+ "can not find the user"
					+ Separator.F_SEPARATOR;
			session.write(result);
			return false;
		}
		
		Obj.User.Builder user = ((Obj.User)u).toBuilder();
		user.setNick(nickname);
		user.setGender(gender);
		//user.toBuilder().setDateOfBirth(date_of_birth);
		//user.toBuilder().setImages(index, builderForValue);
		
		set(Obj.User.class, uid, user.build());
		
		String result = Base64Util.encode(RequestCode.UPDATE_USER_INFO_RESULT.getBytes())
				+ Separator.F_SEPARATOR
				+ Base64Util.encode(RequestCode.SUCCESS.getBytes())
				+ Separator.F_SEPARATOR
				+ Separator.F_SEPARATOR;
		session.write(result);
			
				
		return true;
	}
}
